	
	$(document).ready(function(){
		loadData();
		
		$('.content').css('height',nav.whiceH[1]);
		
		//重置按钮
		$("#resetBtn").click(function(){
			$("#functionName").val("");
			$("#functionCode").val("");
			$("#assemblyName").val("");
			$("#functionImg").val("");
			$("#ordering").val("");
			$("#remark").val("");
			
			$("#functionType").val("MENU");
			$("#state").val("ENABLED");
		});
	});
	
	var orgZTree;
	
	var treeSetting = {	
		data:{
			key:{
				name:"functionName"
			},
			simpleData:{
				enable:true,
				idKey:"functionId",
				pIdKey:"parentFuncId",
				rootPId:0
			}
		},
		callback:{
			onClick:loadDataById
		}
	};

	function loadData(){
	    $.ajax({
	        type: 'post',
	        async: false,
	        url: appCtx + "system/module/loadAll.html",
	        cache: false,
	        dataType : "JSON",
	        success: function (data) {
				if(data==""){
					$("#addNew").show();
				} else {
					$("#addNew").hide();
				}
				orgZTree=$.fn.zTree.init($("#orgTree"), treeSetting, data);
	        }
	    });	
	}

		
	function loadDataById(event,treeId,treeNode){
		var orgId = treeNode.functionId;
		refreshBody(orgId);
	}
	
	function refreshBody(id){
		var url = appCtx + "system/module/queryById.html";
		$.post(url,{id:id},function(data){ 
			$("#orgBody").html(data);
		});
	}

	function addRootData(){
		var url = appCtx + "system/module/init.html";
		$.post(url,{id:0},function(data){ 
			$("#orgBody").html(data);
		});
	}
	
	function addData(){
		var nodes = orgZTree.getSelectedNodes();
		if (nodes.length < 1){
			alert('请选择上级菜单！');
			return ;
		}
		var id = nodes[0].functionId;
		var url = appCtx + "system/module/init.html";
		$.post(url,{id:id},function(data){ 
			$("#orgBody").html(data);
		});
	}
	
    function deleteDataConfirm(){
		var nodes = orgZTree.getSelectedNodes();
		if (nodes.length < 1){
			return ;
		}
		var id = nodes[0].functionId;
	    $.ajax({
	        type: 'post',
	        async: false,
	        url: appCtx + "system/module/delete.html",
			data:{id:id},
	        cache: false,
	        dataType : "JSON",
	        success: function (data) {
				if(data.code == "0"){
					loadData();
					addRootData();
					window.parent.doSuccess();
				}else{
					window.parent.doFail();
				}
	        }
	    });		     
	 }	

	function deleteData(){
		var nodes = orgZTree.getSelectedNodes();
		if (nodes.length < 1){
			alert('请选择要删除的数据！');
			return ;
		}
		
		if(nodes[0]){
			var children = nodes[0].children;
			if(children && children.length>0){
				alert("存在子节点,不允许删除！");
				return;
			}
		}
//		window.parent.doConfirm("确定要删除吗？删除后将无法恢复！",this.name,"deleteDataConfirm()");
		if(window.confirm('确定要删除吗？删除后将无法恢复！')){
			deleteDataConfirm();
         }
	}
		
	function saveData(){
		var functionId = $("#functionId").val();
		var url = appCtx + "system/module/saveModule.html";
		if(functionId != null && functionId != ""){
			url = appCtx + "system/module/updateModule.html";
		}
		
		var flag = $("#addForm").validationAjax();
		if(flag){
		    $.ajax({
		        type: 'post',
		        async: false,
		        url: url,
				data:$("#addForm").serialize(),
		        cache: false,
		        dataType : "JSON",
		        success: function (data) {
		        	if (data.code == "0") {
						global.ajaxLoading.pop('操作成功!',false,2000);
						loadData();
						refreshBody(data.data.page);
					}else if (data.code == "1") {
						alert("操作失败,原因是已经存在相同功能名称!");
						return;
					}  else {
						alert("操作失败!");
						return;
					}
		        }
		    })
		}
		return flag;
	}	